package com.cjack.frenchexcel.controller;

import com.alibaba.fastjson.JSONObject;
import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.config.CommonConfig;
import com.cjack.frenchexcel.config.ErrorCodeDefine;
import com.cjack.frenchexcel.config.LayPage;
import com.cjack.frenchexcel.enumInfo.DataDefineTypeEnum;
import com.cjack.frenchexcel.exception.AdminUserNotFoundException;
import com.cjack.frenchexcel.exception.CommonException;
import com.cjack.frenchexcel.model.DataDefineModel;
import com.cjack.frenchexcel.model.RechargeRecordConditionModel;
import com.cjack.frenchexcel.model.RechargeRecordModel;
import com.cjack.frenchexcel.service.DataDefineService;
import com.cjack.frenchexcel.service.RechargeRecordService;
import com.cjack.frenchexcel.service.UserAdminService;
import com.cjack.frenchexcel.table.AdminUserTable;
import com.cjack.frenchexcel.table.EasyEuroTable;
import com.cjack.frenchexcel.table.RechargeRecordTable;
import com.cjack.frenchexcel.util.ModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/recharge")
public class RechargeExcelEndpoint extends BaseEndpoint{

    @Autowired
    RechargeRecordService service;
    @Autowired
    UserAdminService userAdminService;
    @Autowired
    DataDefineService dataDefineService;

    /**
     * 从excel里面导入数据，分成两步来弄
     * 1、先调用upload接口上传excel文件到服务器
     * 2、用第一步上传返回的excel地址调用此接口
     * 从excel导入数据
     * @param filePath  excel的浏览器端访问路径
     * @return
     */
    @RequestMapping(value = "/importFromExcel", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult importFromExcel(HttpSession session, @RequestParam("filePath") String filePath) {

        try{
            if( StringUtils.isEmpty( filePath)){
                throw new CommonException(ErrorCodeDefine.INTERFACE_COMMON_ERROR, "filePath不能为空");
            }
            Object adminUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            //注意pageDate里面的页数是从0开始计算的
            JSONObject json = service.importFromExcel( filePath, (Long)adminUserId);
            //返回重复的数据
            return AjaxResult.SUCCESS( json);
        }catch ( CommonException e) {
            e.printStackTrace();
            return AjaxResult.ERROR( e.getCode(), e.getMessage());
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 导出excel，分两步走，先生成excel放到对应的目录，再返回生成的excel的地址给浏览器端
     * @param condition
     * @return
     */
    @RequestMapping(value = "/exportAsExcel", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult exportAsExcel( HttpSession session, RechargeRecordConditionModel condition) {

        try{
            Object adminUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            condition.setLoginUserId( (Long)adminUserId);
            JSONObject json = service.exportAsExcel( condition);
            //返回重复的数据
            return AjaxResult.SUCCESS( json);
        }catch ( CommonException e) {
            e.printStackTrace();
            return AjaxResult.ERROR( e.getCode(), e.getMessage());
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult list( HttpSession session, LayPage layPage, RechargeRecordConditionModel condition) {

        try{
            Object loginUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            condition.setLoginUserId( (Long)loginUserId);
            Page< RechargeRecordTable> resultPage = service.list( layPage, condition);
            List< RechargeRecordModel> dataModels = ModelUtils.copyListModel( resultPage.getContent(), RechargeRecordModel.class);
            for( RechargeRecordModel dataModel : dataModels){
                try {
                    AdminUserTable adminUserTable = null;
                    adminUserTable = userAdminService.get( dataModel.getImportUserId());
                    if( adminUserTable != null ){
                        dataModel.setJobNumber( adminUserTable.getJobNumber());
                    }
                    DataDefineModel dataDefineModel = dataDefineService.getByTypeAndKey(DataDefineTypeEnum.RECHARGE_TYPE.name(), dataModel.getRechargeTypeCode());
                    dataModel.setRechargeTypeName( dataDefineModel.getDataValue());
                }catch ( Exception e) {
                    e.printStackTrace();
                }
            }
            layPage.setData( dataModels);
            layPage.setCount( Long.valueOf( resultPage.getTotalElements()).intValue());
            return AjaxResult.SUCCESS( layPage);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/listForEasyEuro", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult listForEasyEuro( LayPage layPage) {

        try{
            Page<EasyEuroTable> resultPage = service.listForEasyEuro( layPage);
            List< RechargeRecordModel> dataModels = ModelUtils.copyListModel( resultPage.getContent(), RechargeRecordModel.class);
            for( RechargeRecordModel dataModel : dataModels){
                AdminUserTable adminUserTable = userAdminService.get( dataModel.getImportUserId());
                if( adminUserTable != null){
                    dataModel.setJobNumber( adminUserTable.getJobNumber());
                }
                DataDefineModel dataDefineModel = dataDefineService.getByTypeAndKey(DataDefineTypeEnum.RECHARGE_TYPE.name(), dataModel.getRechargeTypeCode());
                dataModel.setRechargeTypeName( dataDefineModel.getDataValue());
            }
            layPage.setData( dataModels);
            layPage.setCount( Long.valueOf( resultPage.getTotalElements()).intValue());
            return AjaxResult.SUCCESS( layPage);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 新增
     * @return
     */
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addOrUpdate( RechargeRecordModel model) {
        try{
            if( isAdd( model.getId())){
                model.setCreateTime( new Date());
            }
            model.setUpdateTime( new Date());
            service.add( model);
            return AjaxResult.SUCCESS();
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult del( @RequestParam("ids") String ids) {
        try{
            service.del( ids);
            return AjaxResult.SUCCESS( "删除成功");
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 查询按天统计出对应 到账状态的数据列表
     * @return
     */
    @RequestMapping(value = "/listByDay", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult listByDay( String moneyStatus) {
        try{
            return AjaxResult.SUCCESS( service.listForDay( moneyStatus));
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 按天设置到帐状态
     * @return
     */
    @RequestMapping(value = "/setMoneyStatusByDay", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult setMoneyStatusByDay( String day, String moneyStatus) {
        try{
            service.setMoneyStatusByDay( day, moneyStatus);
            return AjaxResult.SUCCESS( "设置成功");
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
